/*
 * FileReader.cpp
 *
 *  Created on: Nov 20, 2012
 *      Author: William Agnew [agnewwt@muohio.edu]
 */

#include "FileReader.h"
using namespace std;

int rowNum = 0, colNum = 0, rowStart = 0, colStart = 0;


FileReader::FileReader()
{

}

FileReader::FileReader(string fileName, Map *map)
{
	this->fileName = fileName;
	this->map = map;
}

FileReader::~FileReader()
{

}

int FileReader::read()
{
	try
	{
		ifstream inFile(fileName, ios::in);

		if(!inFile.good())
		{
			throw "Error reading file.";
		}

		// Read the words from the text file
		inFile >> rowNum; inFile >> colNum;
		inFile >> rowStart; inFile >> colStart;

		// Build map
		// Set messages
		string str = "";
		for(int i = 0; i < map->getColNum(); i++)
		{
			for(int j = 0; j < map->getRowNum(); j++)
			{
				inFile >> str;
				if(str == SYMBOL_MESSAGE)
				{
					map->position[i][j].setMessage(true);
					map->position[i][j].setSymbol(SYMBOL_MESSAGE);
				}
				else if(str == SYMBOL_NOMESSAGE)
				{
					map->position[i][j].setMessage(false);
					map->position[i][j].setSymbol(SYMBOL_NOMESSAGE);
				}
				else
				{
					throw "Invalid character in file.";
				}
			}
		}
		// Set costs
		str = "";
		float num = 0;
		for(int i = 0; i < map->getColNum(); i++)
		{
			for(int j = 0; j < map->getRowNum(); j++)
			{
				inFile >> num;
				map->position[i][j].setCost(num);

			}
		}
		return 1;
	}
	catch (exception& e)
	{
		cout << "Standard exception: " << e.what() << endl;
		return 0;
	}
	catch(...)
	{
		cerr << "Unable to read " << fileName << endl;
		return 0;
	}
}

void FileReader::printMetrics()
{
	cout << map->getRowNum() << " x " << map->getColNum() << " map," << endl;
	cout << "Starting at (" << map->getRowStart() << "," << map->getColStart() << ")." << endl << endl;
}

Map* FileReader::getMap()
{
	return map;
}
